package com.zero.service;

import com.zero.entity.Family;
import com.zero.entity.FamilyMember;
import com.zero.entity.FamilyMoney;
import com.zero.entity.MemberMoney;
import com.zero.mapper.FamilyMapper;
import com.zero.mapper.FamilyMemberMapper;
import com.zero.mapper.FamilyMoneyMapper;
import com.zero.mapper.MemberMoneyMapper;
import com.zero.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
public class AssetsDebtService {

    @Autowired
    private FamilyMoneyMapper familyMoneyMapper;

    @Autowired
    private MemberMoneyMapper memberMoneyMapper;

    @Autowired
    private FamilyMapper familyMapper;

    @Autowired
    private FamilyMemberMapper familyMemberMapper;

    /**
     * 获得家庭和个人目前资产流出和流入状况
     * @param request
     * @return
     */
    public Result getAssetsDebt(HttpServletRequest request) {
        FamilyMember familyMember = (FamilyMember) request.getAttribute("familyMember");
        if (familyMember == null || familyMember.getId() == null)
            return Result.fail("获取不到用户信息");
        familyMember = familyMemberMapper.selectById(familyMember.getId());
        Family family = familyMapper.selectById(familyMember.getFamilyId());
        HashMap<String, Object> result = new HashMap<>();
        result.put("fAssets", family.getAssets());
        result.put("fDebt", family.getDebt());
        result.put("mAssets", familyMember.getAssets());
        result.put("mDebt", familyMember.getDebt());
        return Result.ok("获取成功", result);
    }
}

